package com.hb.software.controller.salary;

import com.hb.software.controller.BaseController;
import com.hb.software.domain.Result;
import com.hb.software.entity.salary.Salary;
import com.hb.software.salary.SalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@CrossOrigin
@RequestMapping("salary")
public class SalaryController extends BaseController<Salary,Long,SalaryService> {


    private SalaryService salaryService;

    @Autowired
    public void setSalaryService(SalaryService salaryService){
        this.salaryService=salaryService;
        this.manager=this.salaryService;
    }

    /**
     * 查询员工信息
     */
    @PostMapping("/findSalaryAll")
    public Result findSalaryAll(){
        List<Salary> list = salaryService.findSalaryAll();
        try{
            list = salaryService.findSalaryAll();
            return Result.success(list);
        }catch(Exception e){
            e.printStackTrace();
            return Result.error("失败");
        }

    }

    /**
     * 批量删除
     * @param ids
     */
    @GetMapping("/batchDelete")
    public void batchDelete(@RequestParam(value ="ids", required = true)List<Long> ids){
        salaryService.batchDelete(ids);
    }
    /**
     *  通过workerId查找
     */
    @PostMapping("/findById")
    public Result findById(@RequestBody String wid){

        List list=salaryService.findById((Long.valueOf(wid.substring(0,wid.length()-1))));
        return Result.success(list);
    }

    /**
     *  通过姓名查找
     */
    @PostMapping("/findByName")
    public Result findByName(@RequestBody String name) throws UnsupportedEncodingException {
        //处理中文乱码问题
        name = URLDecoder.decode(name,"UTF-8");
        name = name.substring(0,name.length()-1);
//        logger.info(name);
        List list=salaryService.findByName(name);
        return Result.success(list);
    }

    @PostMapping("/exportExcel")
    public Result exportExcel(@RequestBody String path)throws UnsupportedEncodingException {
        //处理中文乱码问题
        path = URLDecoder.decode(path,"UTF-8");
        path = path.substring(0,path.length()-1);
        List list = salaryService.findAll();
        String str = salaryService.createExcel(list,path);
        if(str.equals("success"))
            return Result.success();
        else return Result.error("路径错误");
    }

    @GetMapping("/findDatetime")
    public List<Salary> findDatetime(Date startTime,Date endTime){
        List<Salary> salaries = salaryService.findDatetime(startTime,endTime);
        return salaries;
    }

    @GetMapping("/findDatetimeById")
    public List<Salary> findDatetimeById(Date startTime,Date endTime,Long wid){
        List<Salary> salaries = salaryService.findDatetimeById(startTime,endTime,wid);
        return salaries;
    }


    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
}
